package com.mopa.image;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class ImageProcessorActivity extends Activity {
    /** Called when the activity is first created. */
	private final int CODE_IMAGE = 1;  
	private final int CODE_CAPTURE = 0; 
	private final int CODE_DEL =2; 
	private final static String picUrl="/mnt/sdcard/ptests.jpg";
	private ImageProcessor imagePro;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        RelativeLayout.LayoutParams relativeParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
//        RelativeLayout relativeLayout=new RelativeLayout(this);
//        
//        relativeParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        ImageView mImgPic=new ImageView(this);
//        BitmapFactory.Options options=new BitmapFactory.Options(); 
//        options.inJustDecodeBounds=true;
//        BitmapFactory.decodeFile(picUrl,options);
//        options.inSampleSize = computeSampleSize(options.outWidth, options.outHeight);
//        Log.e("debug", "<options.outWidth,options.outHeight,options.inSampleSize>"+options.outWidth+":"+options.outHeight+":"+options.inSampleSize);
//        options.inJustDecodeBounds = false;
//        options.inPreferredConfig = Config.ARGB_8888;
//        Bitmap bm=BitmapFactory.decodeFile(picUrl,options);
//        
//        Matrix m = new Matrix();
//        int rotation = getJpgRotation(picUrl);
//		m.setRotate(rotation);
//		bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, false);
//        imagePro=new ScaleImageProcessor(480, 640, ScaleType.CENTER_INSIDE);
//        bm=imagePro.processImage(bm);
//        mImgPic.setImageBitmap(bm);
//        relativeLayout.addView(mImgPic,relativeParams);
//        setContentView(relativeLayout);
        setContentView(R.layout.main);
        showCamPopMenu();
    }
    public void showCamPopMenu()
    {
    	final DialogInterface.OnClickListener onItemClickLintener=new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch (which) {
				case CODE_CAPTURE:
					Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					ImageProcessorActivity.this.startActivityForResult(intent, CODE_CAPTURE);
					break;
				case CODE_IMAGE:
					intent=new Intent(Intent.ACTION_GET_CONTENT);
					intent.setType("image/*");
					ImageProcessorActivity.this.startActivityForResult(intent, CODE_IMAGE);
					break;
				case CODE_DEL:
					break;
				default:
					break;
				}
				
			}
		};
    	String []itemName=new String[3];
    	itemName[0]="Camera";
    	itemName[1]="Photo Albums";
    	itemName[2]="Delete";
    	AlertDialog.Builder menuDialog=new AlertDialog.Builder(ImageProcessorActivity.this);
    	menuDialog.setTitle("Settings");
		menuDialog.setItems(itemName,onItemClickLintener);
		menuDialog.show();
    }
    
	public int computeSampleSize(int weight, int height)
	{
		int bigOne = weight>height?weight:height;
		return bigOne/640;
	}
    public int getJpgRotation(String picUrl)
	{
		if(picUrl == null)
			return 0;
		if(picUrl.endsWith(".jpg") == false && picUrl.endsWith(".JPG") ==  false && picUrl.endsWith(".dat") == false)
			return 0;
		ExifInterface exif;
		try {
			exif = new ExifInterface(picUrl);
			String rotateStr = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
			if(rotateStr != null && rotateStr.length() > 0)
			{
				int rotate =  Integer.parseInt(rotateStr);
				switch(rotate)
				{
				case ExifInterface.ORIENTATION_ROTATE_90:
					return 90;
				case ExifInterface.ORIENTATION_ROTATE_180:
					return 180;
				case ExifInterface.ORIENTATION_ROTATE_270:
					return 270;
				}
			}
		} catch (Exception e) {
		}
		return 0;
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		//super.onActivityResult(requestCode, resultCode, data);
		Log.e("debug", "resultCode:"+resultCode);
		if (resultCode != RESULT_OK) {  
	        Log.e("debug","ActivityResult resultCode error");  
	        return;  
	    }  
	    Bitmap bm = null;  
	    ContentResolver resolver = getContentResolver();  
	    if (requestCode == CODE_IMAGE) {  
	        bm = showPhoto(data, bm, resolver);  
	    }  
	    if (requestCode == CODE_CAPTURE && resultCode==RESULT_OK){  
	       bm = showPhoto(data, bm, resolver);  
	    } 
	}
	
	private Bitmap showPhoto(Intent data, Bitmap bm, ContentResolver resolver) {  
		try {  
		    Uri originalUri = data.getData();  
		    bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);  
		    String[] proj = {MediaStore.Images.Media.DATA};  
		    Cursor cursor = managedQuery(originalUri, proj, null, null, null);   
		    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
		    cursor.moveToFirst();  
		    String path = cursor.getString(column_index);
		    //analyseCurrentSelectImage(bm);
		    Log.i("debug",path+" "+bm.getHeight()+bm.getWidth());   
		}catch (IOException e) {  
		    Log.e("debug",e.toString());   
		}  
		return bm;  
		}  
    
}